from django.db import models
from django.core.validators import FileExtensionValidator
from users.services import get_path_upload_photo, validate_size_image, get_path_upload_reward
from users.models import User


class Tasks(models.Model):
    """ Task model """

    class Status(models.TextChoices):
        """ choice Wish status
        """
        ACTIVE = 'AC', 'Active'
        DONE = 'DN', 'Done'
        EMPTY = 'EM', 'Empty'

    owner = models.ForeignKey(User, on_delete=models.CASCADE, related_name='Пользователь')
    title = models.CharField(max_length=35, blank=True, null=True, verbose_name='Заголовок')
    description = models.CharField(max_length=300, blank=True, null=True, verbose_name='Описание')
    creation_date = models.DateTimeField(auto_now_add=True)
    is_done = models.BooleanField(verbose_name='Выполнено', default=False)
    ended_date = models.DateField(blank=True, null=True)
    done_comment = models.CharField(max_length=300, verbose_name='Комментарий', blank=True, null=True)
    year = models.CharField(max_length=4, blank=True, null=True, verbose_name='Год создания')
    remind_by_date = models.DateField(blank=True, null=True, verbose_name='Напоминание')
    status = models.CharField(
        max_length=2, 
        verbose_name='Статус', 
        choices=Status.choices, 
        default=Status.EMPTY
        )
    start_img = models.ImageField(
        upload_to=get_path_upload_photo,
        blank=True,
        null=True,
        verbose_name='начальное изображение',
        validators=[FileExtensionValidator(allowed_extensions=['jpg', 'JPG', 'JPEG', 'PNG', 'bmp', 'png', 'jpeg']), validate_size_image]
    )
    end_img = models.ImageField(
        upload_to=get_path_upload_photo,
        blank=True,
        null=True,
        verbose_name='итоговое изображение',
        validators=[FileExtensionValidator(allowed_extensions=['jpg', 'JPG', 'JPEG', 'PNG', 'bmp', 'png', 'jpeg']), validate_size_image]
    )
    reward = models.ForeignKey('Reward', on_delete=models.DO_NOTHING, blank=True, null=True)

    class Meta:
        verbose_name = 'Задача'
        verbose_name_plural = 'Задачи'
        ordering = ['id',]
    
    def __str__(self) -> str:
        return f'{self.owner} - {self.title}'


class Comment(models.Model):
    """ Users Comments from website """

    author = models.CharField(max_length=150, verbose_name='Автор Комментария', default='Unauthorized') 
    body = models.CharField(max_length=500, default=None)
    created = models.DateTimeField(auto_now_add=True)

    class Meta:
        verbose_name = 'Комментарий'
        verbose_name_plural = 'Комментарии'
        ordering = ['created']
    
    def __str__(self) -> str:
        return f'Комментарий от {self.author} от {self.created}'


class Reward(models.Model):
    """ Rewards for successfully completing tasks  """

    reward_name = models.CharField(max_length=100, verbose_name='Награда')
    slug = models.SlugField(max_length=100)
    reward_img = models.ImageField(validators=[validate_size_image, ], upload_to=get_path_upload_reward,)

    def __str__(self) -> str:
        return f'Награда: {self.reward_name}'
    
    class Meta:
        verbose_name = 'Награда'
        verbose_name_plural = 'Награды'
